package top.heue.epro.util;

import android.content.Context;
import org.json.JSONException;
import org.json.JSONObject;
import top.heue.epro.bean.ConfigBean;

public class ConfigUtil {
    private static final String fileName = "/EYE.CFG";
    
    public static String getConfigPath(Context context) {
        //return context.getFilesDir().getAbsolutePath() + fileName;
        return context.getExternalFilesDir("config").getAbsolutePath() + fileName;
    }
    
    public static ConfigBean readUtil(Context context) {
        String config = getConfigPath(context);
        config = FileIOUtil.read(config);
        try {
            JSONObject obj = new JSONObject(config);
            int i1,i2;
            String s1;
            i1 = obj.getInt(ConfigBean.BRIGHTNESS);
            s1 = obj.getString(ConfigBean.SRC);
            i2 = obj.getInt(ConfigBean.ALPHA);
            return new ConfigBean(i1, s1, i2);
        } catch (JSONException e) {
            //LOG.i("配置读取异常", e);
            return null;
        }
    }

    public static void writeUtil(Context context, ConfigBean cfgb) {
        try {
            JSONObject obj = new JSONObject();
            obj.put(ConfigBean.BRIGHTNESS, cfgb.getBrightness());
            obj.put(ConfigBean.SRC, cfgb.getSrc());
            obj.put(ConfigBean.ALPHA, cfgb.getAlpha());
            String config = jsonFormat(obj.toString());
            String path = getConfigPath(context);
            FileIOUtil.write(path, config);
        } catch (JSONException e) {
            //LOG.i("配置写入异常", e);
        }
    }

    //格式化json
    private static String jsonFormat(String str) {
        str = str.replace(",", ",\n");
        str = str.replace("{", "{\n");
        str = str.replace("}", "\n}");
        return str;
    }
}
